local zhaofu = fk.CreateSkill{
  name = "ls__zhaofu",
  tags = { Skill.Lord },
}

Fk:loadTranslationTable{
  ["ls__zhaofu"] = "诏缚",
  [":ls__zhaofu"] = "主公技，轮次开始时，你可横置至多X名吴势力角色攻击范围内的角色（X为吴势力角色数），当这些角色重置后，你摸一张【影】。",
  ["#ls__zhaofu-choose"] = "诏缚：你可横置至多%arg名吴势力攻击范围内的角色",

  ["$ls__zhaofu1"] = "大吴将士，速速接诏讨贼！",
  ["$ls__zhaofu2"] = "乱臣贼子，还不束手就擒！",
}

local U = require "packages/utility/utility"

zhaofu:addEffect(fk.RoundStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(zhaofu.name)
  end,
  on_cost = function(self, event, target, player, data)
    local n = #table.filter(player.room:getAlivePlayers(player), function (p) return p.kingdom == "wu" end)
    local players = {}
    for _, p in ipairs(player.room:getAlivePlayers(player)) do
      if p.kingdom == "wu" then
        for _, sp in ipairs(player.room:getAlivePlayers(player)) do
          if p:inMyAttackRange(sp) and not sp.chained then
            table.insertIfNeed(players, sp)
          end
        end
      end
    end
    local to = player.room:askToChoosePlayers(player, {
      skill_name = zhaofu.name,
      targets = players,
      min_num = 1,
      max_num = n,
      prompt = "#ls__zhaofu-choose:::"..n,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    for _, p in ipairs(event:getCostData(self).tos) do
      if not p.chained then
        p:setChainState(true)
        player.room:addPlayerMark(p, "zhaofu-chained")
      end
    end
  end,
})

zhaofu:addEffect(fk.ChainStateChanged, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(zhaofu.name) and target:getMark("zhaofu-chained") > 0 and not target.chained
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "zhaofu-chained", 0)
    local id = U.getShade(room, 1)
    room:obtainCard(player, id, true, fk.ReasonJustMove, nil, zhaofu.name)
  end,
})

return zhaofu